package com.xuanwu.shop.dao.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class BaseDao {

	protected Connection conn; // 连接对象
	protected PreparedStatement psta; // 预编译处理对象
	protected ResultSet rs; // 结果集对象
	protected Statement sta; // 

	/**
	 * 获取连接对象
	 * 
	 * @return Connection对象
	 */
	public Connection getConnection() {
		Connection conn = null;
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=test_data", "sa",
					"zhang520");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}

	/**
	 * 关闭连接对象
	 * 
	 * @param conn
	 *            Connection对象
	 * @param sta
	 *            Statment对象
	 * @param rs
	 *            ResultSet对象
	 */
	public void close(Connection conn, Statement sta, ResultSet rs) {
		try {
			if (rs != null && !rs.isClosed()) {
				rs.close();
			}
			if (sta != null && !sta.isClosed()) {
				sta.close();
			}
			if (conn != null && !conn.isClosed()) {
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			rs = null;
			sta = null;
			conn = null;
		}

	}

	public static void main(String[] args) {
		BaseDao test = new BaseDao();
		Connection conn = test.getConnection();
		System.out.println(conn);
	}

}
